*Data sets for the graphs with Maturation 
*** BE CAREFUL:

*LANGUAGE
* With scale invariance 
global path "Y:\Fuyao_Erlfang\estimates\maturation\lang_add"
* Last update 09/17/21 eliminate outliers variance level 8-10
*global path "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_add""
use "$path\bootsresults.dta", clear 
  
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) // Remove 1.4	
foreach num of numlist 9/17{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	// Remove 1.4	
}
foreach num of numlist 18/26 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 33/41{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var26_2 var27-var32 var33_2-var41_2 var42 var43{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

/* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var23_2 var24_2 var25_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
*/
keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 27/32 42/43{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/26 33/41{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path\point_estimates_add_mat.dta"
drop _merge
drop additive 
rename m1 add_pe
keep if q==1
gen var=""
replace var ="Kbar" if n>7 & n<18
replace var=" $\sigma_{s,\ell}^{2}$" if n<27 & var==""
replace var="Delta" if var==""
drop if n>26 & n<33
drop if n<8
drop if n>41
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n var var1 level add_pe

*Last updated :
saveold "Y:\Fuyao_Erlfang\estimates/maturation/Graphs/l/l_add.dta" , replace



 
**# Multiplicative#
* 
global path "Y:\Fuyao_Erlfang\estimates\maturation\lang_mul"

use "$path/bootsresults.dta", clear
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) // Remove 1.4	
foreach num of numlist 9/16{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	// Remove 1.4		
}
foreach num of numlist 17/25 32{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 33/41{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*6		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var25_2 var26-var31 var32_2-var41_2 var42 var43{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 26/31 42/43{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/25 32/41{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/point_estimates_mul_mat.dta"
drop _merge
drop multiplicative 
rename m2 mul_pe
keep if q==1
cap drop var
gen var=""
replace var ="Kbar" if n>7 & n<17
replace var=" $\sigma_{s,\ell}^{2}$" if n<26 & var==""
replace var="Delta" if var==""
drop if n>25 & n<33
drop if n<8
drop if n>41
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n var var1 level mul_pe


*Last Updated: 09/22/21
saveold "Y:\Fuyao_Erlfang\estimates/maturation/Graphs/l/l_mul.dta" , replace


* WithOut scale invariance 
**# Additive #
*

global path "Y:\Fuyao_Erlfang\estimates\maturation\lang_level_add"
	use "$path\bootsresults.dta", clear 
	 
	* Creating the transformations:
	foreach num of numlist 2/4{
	gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
	}
	gen var5_2=(1-1.4^var5)

	gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

	gen var8_2=(var8) //Remove the 1.4
	foreach num of numlist 9/17{
	local j = `num'-1
	gen var`num'_2=var`j'_2+(var`num')	//Remove the 1.4	
	}
	foreach num of numlist 18/26 {
	gen var`num'_2=(1.4^var`num')		
	}
	foreach num of numlist 33/41{
	gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
	}
	cap drop q
	foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var26_2 var27-var32 var33_2-var41_2 var42-var53{
	sort `var'
	gen q=_n
	gen aux= `var' if q==25
	egen `var'_lb= max(aux) 
	drop aux
	gen aux= `var' if q==975
	egen `var'_ub= max(aux) 

	drop aux
	sum `var'
	gen aux= r(sd)
	egen `var'_sd= max(aux) 

	drop q aux
	}



	keep var*sd var*lb var*ub 
	gen q=_n
	foreach num of numlist 1 6 27/32 42/53{
		rename var`num'_lb var_lb_`num'
		rename var`num'_ub var_ub_`num'
		rename var`num'_sd var_sd_`num'
		
	}
	foreach num of numlist 2/5 7/26 33/41{
		rename var`num'_2_lb var_lb_`num'
		rename var`num'_2_ub var_ub_`num'
		rename var`num'_2_sd var_sd_`num'
		
	}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path\point_estimates_add_mat_wo.dta"
drop _merge
drop additive 
rename m1 add_pe
keep if q==1
cap drop var
gen var=""
replace var ="Kbar" if n>7 & n<18
replace var=" $\sigma_{s,\ell}^{2}$" if n<27 & var==""
replace var="Delta" if  n>32 & n<42 & var==""
replace var="Beta1" if  n>41 & n<52 & var==""
drop if n>26 & n<33
drop if n>51
drop if n<8
drop q
drop var1 
rename var2 var1
gen level=substr(var1,-2,.)
destring level, replace
order  n  var var1 level add_pe

*Last Updated: 09/22/21
saveold "Y:\Fuyao_Erlfang\estimates/maturation/Graphs/l/l_add_wo_si.dta" , replace

**# Multiplicative #
* 
global path "Y:\Fuyao_Erlfang\estimates\maturation\lang_level_mul"
use "$path/bootsresults.dta", clear
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //Remove the 1.4
foreach num of numlist 9/16{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Remove the 1.4	
}
foreach num of numlist 17/25 32{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 33/41{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*6		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var25_2 var26-var31 var32_2-var41_2 var42-var53{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 26/31 42/53{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/25 32/41{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/point_estimates_mul_mat_wo.dta"
drop _merge
drop multiplicative 
rename  m2 mul_pe
keep if q==1
* CAREFULL CHECK THIS IS CORRECT BEFORE RUNNING
cap drop var
gen var=""
replace var ="Kbar" if n>7 & n<17
replace var=" $\sigma_{s,\ell}^{2}$" if n<26 & var==""
replace var="Delta" if  n>32 & n<42 & var==""
replace var="Beta1" if  n>41 & n<52 & var==""
drop if n>25 & n<33
drop if n>51
drop if n<8
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n  var var1 level mul_pe

/*** Be careful drop this when we have final results!!!
foreach i of numlist 16{
 replace var_ub_ = mul_pe + 1.96*var_sd_ if n==`i'
 replace var_lb_ = mul_pe - 1.96*var_sd_ if n==`i'	
	
}
*/
* Last Updated: 09/22
saveold "Y:\Fuyao_Erlfang\estimates\maturation/Graphs/l/l_mul_wo_si.dta" , replace


